package com.yang.jpa.controller;

import com.yang.jpa.model.CheckAttendanceSheet;
import com.yang.jpa.service.AttendanceSheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class AttendanceSheetController {

    @Autowired
    private AttendanceSheetService attendanceSheetService;

    @PostMapping("/toExamine")
    public Map<String, Object> toExamine(@RequestParam(name = "checkAttendanceSheetId") String checkAttendanceSheetId){
        return attendanceSheetService.toExamine(Integer.valueOf(checkAttendanceSheetId));
    }

    @PostMapping("/findCheckAttendanceSheet")
    public List<CheckAttendanceSheet> findCheckAttendanceSheet(@RequestParam(name = "staffId") String staffId){
        return attendanceSheetService.findCheckAttendanceSheet(Integer.valueOf(staffId));
    }

    @PostMapping("/passAudit")
    public Map<String, Object> passAudit(@RequestParam(name = "checkAttendanceSheetId") String checkAttendanceSheetId){
        return attendanceSheetService.passAudit(Integer.valueOf(checkAttendanceSheetId));
    }

    @PostMapping("/submit")
    public Map<String, Object> submit(@RequestParam(name = "type")String type,
                                      @RequestParam(name = "staffId")String staffId,
                                      @RequestParam(name = "staffName")String staffName,
                                      @RequestParam(name = "attendanceSheetDate")String attendanceSheetDate,
                                      @RequestParam(name = "startTime")String startTime,
                                      @RequestParam(name = "endTime")String endTime,
                                      @RequestParam(name = "duration")String duration) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return attendanceSheetService.submit(Integer.parseInt(type), Integer.valueOf(staffId), staffName, attendanceSheetDate,  sdf.parse(startTime), sdf.parse(endTime), Integer.parseInt(duration));
    }
}
